﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CorporateGymManagementSystem.Binding_classes;
using BLL;
using BLL.Entities;

namespace CorporateGymManagementSystem
{
    /// <summary>
    /// Interaction logic for ViewMessageWindow.xaml
    /// </summary>
    public partial class ViewMessageWindow : Window
    {
        private InboxMessageDataGridItem inboxMessageItem;
        private OutboxMessageDataGridItem outboxMessageItem;
        GymWorker loggedInUser;
        
        public ViewMessageWindow(InboxMessageDataGridItem messageItem, GymWorker gymWorker)
        {
            InitializeComponent();
            loggedInUser = gymWorker;
            this.inboxMessageItem = messageItem;
            fromLabel.Content = messageItem.Sender.FullName;
            subjectLabel.Content = messageItem.Subject;
            toLabel.Content = loggedInUser.FullName;
            contentTextBlock.Text = messageItem.Content;
            dateLabel.Content = messageItem.Date.ToString("dd/MM/yyyy HH:mm:ss");
            
            
        }

        public ViewMessageWindow(OutboxMessageDataGridItem messageItem, GymWorker gymWorker)
        {
            InitializeComponent();
            this.outboxMessageItem = messageItem;
            loggedInUser = gymWorker;
            fromLabel.Content = loggedInUser.FullName; 
            subjectLabel.Content = outboxMessageItem.Subject;
            StringBuilder receiversNames = new StringBuilder();
            foreach (GymWorker receiver in messageItem.Receivers)
            {
                receiversNames.Append(receiver.FullName).Append(";");
            }
            toLabel.Content = receiversNames.ToString();
            contentTextBlock.Text = outboxMessageItem.Content;
            dateLabel.Content = messageItem.Date.ToString("dd/MM/yyyy HH:mm:ss");
            toolBarReplyButton.IsEnabled = false;
            
        }

        public ViewMessageWindow(Message messageboardMessage)
        {
            InitializeComponent();
            GymWorker publisher = GymWorkerManager.Instance.GetWorkerById(messageboardMessage.SenderId);
            fromLabel.Content = publisher.FullName;
            subjectLabel.Content = messageboardMessage.Subject;
            contentTextBlock.Text = messageboardMessage.Text;
            dateLabel.Content = messageboardMessage.CreateTime.ToString("dd/MM/yyyy HH:mm:ss");
            toolBarForwardButton.IsEnabled = false;
            toolBarReplyButton.IsEnabled = false;
        }

        private void toolBarReplyButton_Click(object sender, RoutedEventArgs e)
        {
            ComposeNewMessageWindow composeNewMessageWindow = new ComposeNewMessageWindow(loggedInUser, inboxMessageItem.Sender, inboxMessageItem.Subject, inboxMessageItem.Content, eComposeType.RegularMessage);
            bool? res = composeNewMessageWindow.ShowDialog();
        }

        private void toolBarForwardButton_Click(object sender, RoutedEventArgs e)
        {
            if (inboxMessageItem != null)
            {
                ComposeNewMessageWindow composeNewMessageWindow = new ComposeNewMessageWindow(loggedInUser, inboxMessageItem.Subject, inboxMessageItem.Content, eComposeType.RegularMessage);
                bool? res = composeNewMessageWindow.ShowDialog();
            }
            else
            {
                ComposeNewMessageWindow composeNewMessageWindow = new ComposeNewMessageWindow(loggedInUser, outboxMessageItem.Subject, outboxMessageItem.Content, eComposeType.RegularMessage);
                bool? res = composeNewMessageWindow.ShowDialog();
            }
        }

        private void ButtonBar_Loaded_1(object sender, RoutedEventArgs e)
        {
            ToolBar toolBar = sender as ToolBar;
            var overflowGrid = toolBar.Template.FindName("OverflowGrid", toolBar) as FrameworkElement;
            if (overflowGrid != null)
            {
                overflowGrid.Visibility = Visibility.Collapsed;
            }
        }
    }
}
